package org.csu.jpetstore.controller;

import org.csu.jpetstore.bo.CartBO;
import org.csu.jpetstore.bo.CartItemBO;
import org.csu.jpetstore.bo.ItemBO;
import org.csu.jpetstore.common.CommonResponse;
import org.csu.jpetstore.common.ResponseCode;
import org.csu.jpetstore.service.CartService;
import org.csu.jpetstore.service.ItemService;
import org.csu.jpetstore.service.TokenService;
import org.csu.jpetstore.util.MyToken;
import org.csu.jpetstore.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author huangzhangyan
 */
@Controller
@RequestMapping(value = "/carts")
public class CartController {
    @Autowired
    private ItemService itemService;
    @Autowired
    private CartService cartService;
    @Autowired
    private TokenService tokenService;

    // 根据用户名 获取用户购物车
    @RequestMapping(value = "/account/{username}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResponse<CartVO> getCartByUsername(
            @RequestHeader("Authorization") MyToken token,
            @PathVariable String username) {
        // 空校验
        if (null == username || "".equals(username)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "用户名不能为空");
        }
        // token校验
        if (!tokenService.isUserIdEqualToken(username, token)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "Token校验失败,请重新登录");
        }
        CartVO cartVO = CartBO.toCartVO(cartService.getCartByUsername(username));

        return CommonResponse.createForSuccess(cartVO);
    }

    //修改用户购物车物品数量 (数量设为0时为删除)
    @RequestMapping(value = "/account/{username}/cart_item", method = RequestMethod.POST)
    @ResponseBody
    public CommonResponse<CartVO> updateCartQuantity(
            @RequestHeader("Authorization") MyToken token,
            @PathVariable("username") String username,
            @RequestParam("itemID") String itemId,
            @RequestParam("itemQuantity") Integer quantity
    ) {
        // 空校验
        if (null == username || "".equals(username)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "用户名不能为空");
        }
        if (null == itemId || null == quantity) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "参数不能为空");
        }

        // token校验
        if (!tokenService.isUserIdEqualToken(username, token)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "Token校验失败,请重新登录");
        }

        // 参数校验
        if (quantity < 0) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "参数错误,数量不能小于0");
        }
        ItemBO itemBO = itemService.getItemBOById(itemId);
        if (itemBO == null) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "参数错误,Id 为" + itemId + "的Item不存在");
        }

        CartItemBO cartItemBO = CartItemBO.toCartItemBO(itemBO, quantity);
        cartService.setCartQuantity(username, cartItemBO);
        CartVO cartVO = CartBO.toCartVO(cartService.getCartByUsername(username));
        return CommonResponse.createForSuccess(cartVO);
    }

    //添加一个商品
    @RequestMapping(value = "/account/{username}/addOne", method = RequestMethod.POST)
    @ResponseBody
    public CommonResponse<CartVO> addOne(
            @RequestHeader("Authorization") MyToken token,
            @RequestParam String itemId,
            @PathVariable String username) {
        // 空校验
        if (null == username || "".equals(username)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "用户名不能为空");
        }
        if (null == itemId) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "参数不能为空");
        }

        // token校验
        if (!tokenService.isUserIdEqualToken(username, token)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "Token校验失败,请重新登录");
        }

        ItemBO itemBO = itemService.getItemBOById(itemId);
        if (itemBO == null) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "参数错误,Id 为" + itemId + "的Item不存在");
        }

        CartItemBO cartItemBO = cartService.getCartItemBOByUsernameAndItemId(username, itemId);
        if (cartItemBO == null || cartItemBO.getQuantity() == 0) {
            cartItemBO = CartItemBO.toCartItemBO(itemBO, 1);
            cartService.setCartQuantity(username, cartItemBO);
            CartVO cartVO = CartBO.toCartVO(cartService.getCartByUsername(username));
            return CommonResponse.createForSuccess(cartVO);
        }

        if (cartItemBO.getQuantity() > 0) {
            cartItemBO = CartItemBO.toCartItemBO(itemBO, cartItemBO.getQuantity() + 1);
            cartService.setCartQuantity(username, cartItemBO);
            CartVO cartVO = CartBO.toCartVO(cartService.getCartByUsername(username));
            return CommonResponse.createForSuccess(cartVO);
        }
        return null;
    }
}
